//
//  SEGOpenDoorNoticeChooseViewController.m
//  YYKitDemo
//
//  Created by 刘山 on 2017/11/8.
//  Copyright © 2017年 Sam. All rights reserved.
//

#import "SEGOpenDoorNoticeChooseViewController.h"
#import "SEGTextCell.h"
#import "SoundService.h"
#import "SEGSoundModel.h"

@interface SEGOpenDoorNoticeChooseViewController ()<UITableViewDelegate, UITableViewDataSource,SEGTextCellDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *layouts;
@property (nonatomic,strong)UIButton* leftButton;//无导航时候的左边按钮

@end

@implementation SEGOpenDoorNoticeChooseViewController{
    NSArray<NSArray<SEGSoundModel*>*>* dataSource;
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if (self.isHideBar) {
        [self.navigationController setNavigationBarHidden:NO animated:animated];
    }
}
//- (void)viewWillDisappear:(BOOL)animated {
//    [super viewWillDisappear:animated];
//    if (self.isHideBar) {
//        self.navigationController.navigationBarHidden=YES;
//    }
//}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navDisplayType = SEGNavigationBarDisplayTypeIgnore;
    self.title = @"设置开门提示音";
    self.view.backgroundColor = kColor_bg;
    [self showCustomNavigationBackButton];
    
    if ([self respondsToSelector:@selector(setAutomaticallyAdjustsScrollViewInsets:)]) {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    
    dataSource = [SEGSoundModel seg_getSoundData];
    if (!dataSource) {
        dataSource = [SEGSoundModel seg_initData];
    }
    
    _layouts = [NSMutableArray array];
    
    [self refreshData];
    [self.view addSubview:self.tableView];
    [_tableView reloadData];
}

-(void)refreshData{
    _layouts = [NSMutableArray array];
    [dataSource enumerateObjectsUsingBlock:^(NSArray<SEGSoundModel *> * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSMutableArray *arrM = [NSMutableArray array];
        [obj enumerateObjectsUsingBlock:^(SEGSoundModel * _Nonnull obj1, NSUInteger idx1, BOOL * _Nonnull stop1) {
            if ([obj1.kitType isEqualToString:@"3"]) {
                NSString *string = [[NSUserDefaults standardUserDefaults] valueForKey:kPlayOpenDoorSoundKey];
                obj1.activity = (string && [string isEqualToString:@"1"]) ? @"1" : @"0";
            }
            [arrM addObject: [[SEGDoorTextLayout alloc]initWithContent:obj1.name ShowType:[obj1.kitType integerValue] activity:obj1.activity]];
        }];
        [_layouts addObject:arrM];
    }];
}

#pragma mark - UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    SEGSoundModel *data = dataSource.firstObject.firstObject;
    if ([data.activity isEqualToString:@"0"]) {
        return 1;
    }
    return _layouts.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray *data = _layouts[section];
    return data.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *cellID = @"cell";
    SEGTextCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[SEGTextCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    NSArray *data = _layouts[indexPath.section];
    [cell setLayout:data[indexPath.row]];
    cell.switchButton.hidden = YES;
    cell.titleImageView.hidden = YES;
    if (indexPath.section != 0) {
        cell.titleImageView.hidden = [dataSource[indexPath.section][indexPath.row].activity isEqualToString:@"0"] ? YES : NO;
        cell.contentLabel.textColor = kColor_S3;
    }
    if (indexPath.section == 0) {
        cell.switchButton.hidden = NO;
        [cell.switchButton setOn:[dataSource[indexPath.section][indexPath.row].activity isEqualToString:@"0"] ? NO : YES];
    }
    cell.delegate = self;
    cell.indexPath = indexPath;
    cell.backgroundColor = kColor_W;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return CGFLOAT_MIN;
    }
    return 44.0;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return nil;
    }
    UIView *backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, LYScreenWidth(), 44)];
    backView.backgroundColor = kColor_bg;
    
    UILabel *lb = [[UILabel alloc]initWithFrame:CGRectMake(15, 15, LYScreenWidth(), 0)];
    lb.font = [UIFont systemFontOfSize:13];
    lb.textColor = kColor_3;
    lb.text = @"提示音";
    [lb sizeToFit];
    [backView addSubview:lb];
    
    return backView;
}
#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSArray *data = _layouts[indexPath.section];
    return ((SEGDoorTextLayout *)data[indexPath.row]).height;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return CGFLOAT_MIN;
}

- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
    return NO;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [[SoundService sharedSoundService]doorOpenMsg];
}

-(void)seg_CellClickAtIndexPath:(NSIndexPath*)indexPath{
    if (indexPath.section == 0) {
        return;
    }
    [dataSource[indexPath.section] enumerateObjectsUsingBlock:^(SEGSoundModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        obj.activity = @"0";
    }];
    
    dataSource[indexPath.section][indexPath.row].activity = @"1";
    [SEGSoundModel seg_saveSoundDataWithData:dataSource];
    [[SoundService sharedSoundService]doorOpenMsg];
    [self refreshData];
    [self.tableView reloadData];
}

-(void)seg_CellSwitch:(BOOL)isOn{
    [dataSource enumerateObjectsUsingBlock:^(NSArray<SEGSoundModel *> * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (idx == 0) {
            [obj enumerateObjectsUsingBlock:^(SEGSoundModel * _Nonnull obj1, NSUInteger idx1, BOOL * _Nonnull stop1) {
                obj1.activity = isOn ? @"1" : @"0";
                [[NSUserDefaults standardUserDefaults] setObject:obj1.activity forKey:kPlayOpenDoorSoundKey];
                [[NSUserDefaults standardUserDefaults] synchronize];
                *stop = YES;
            }];
        }
    }];
    
    [SEGSoundModel seg_saveSoundDataWithData:dataSource];
    [self.tableView reloadData];
}

-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [UITableView new];
        _tableView.frame =self.view.bounds;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.scrollIndicatorInsets = _tableView.contentInset;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.backgroundView.backgroundColor = [UIColor clearColor];
        _tableView.tableFooterView = [UIView new];
        _tableView.separatorColor = kColor_L2;
        _tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, -15);
    }
    return  _tableView;
}

-(void)left{
    UIImage* image = [UIImage imageNamed:SEGPicName(@"back")];
    UIButton* leftButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, image.size.width, image.size.height)];
    [leftButton setImage:image forState:UIControlStateNormal];
    [leftButton setImage:image forState:UIControlStateHighlighted];
    [leftButton addTarget:self action:@selector(onNavigationLeftButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
}

-(NSArray*)data{
    return @[
             @[
                 @{@"name" : @"开门提示音",@"type" : @"3"}
                 ],
             @[
                 @{@"name" : @"普通话",@"type" : @"4",@"soundname":@"a1.mp3"},
                 @{@"name" : @"湖南话",@"type" : @"4",@"soundname":@"a2.mp3"},
                 @{@"name" : @"湖北话",@"type" : @"4",@"soundname":@"a3.mp3"},
                 @{@"name" : @"湖北话1",@"type" : @"4",@"soundname":@"a4.mp3"}
               ]
             ];
}
@end
